package com.glory.hikvisionmobile.adapter;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.App;
import com.glory.hikvisionmobile.bean.DataCollect.DcModelBean;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * @author yi.zhang
 * @date 2019/12/9
 * @desc
 */
public class DataCollectDetailsAdapter extends BaseQuickAdapter<DcModelBean,BaseViewHolder> {

    private String minValue = "";
    private String maxValue = "";

    public DataCollectDetailsAdapter(int layoutResId, @Nullable List<DcModelBean> data,String minValue,String maxValue) {
        super(layoutResId, data);
        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    @Override
    protected void convert(BaseViewHolder helper, DcModelBean item) {
        helper.setText(R.id.sequence_Tv,item.getDcName());
        EditText et = helper.getView(R.id.inputValue_Edt);
        if (et.getTag() instanceof TextWatcher) {
            et.removeTextChangedListener((TextWatcher) et.getTag());
        }
        et.setText(item.getDcData());
        TextWatcher tw = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
                    try {
                        double value = Double.parseDouble(s.toString());
                        compare(value,et);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        et.setTextColor(App.app.getResources().getColor(R.color.red));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setDcData(s.toString());
            }
        };

        try {
            double value = Double.parseDouble(item.getDcData());
            compare(value,et);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            et.setTextColor(App.app.getResources().getColor(R.color.red));
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
            et.setTextColor(App.app.getResources().getColor(R.color.red));
        }
        et.addTextChangedListener(tw);
        et.setTag(tw);
    }

    private void compare(double value, EditText view){
        if (FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
            view.setTextColor(App.app.getResources().getColor(R.color.black));
        }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double maxValue = Double.parseDouble(this.maxValue);
            double minValue = Double.parseDouble(this.minValue);
                if(minValue<=value&&value<=maxValue) {
                    view.setTextColor(App.app.getResources().getColor(R.color.black));
                }else{
                    view.setTextColor(App.app.getResources().getColor(R.color.red));
                }
        }else if (FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double minValue = Double.parseDouble(this.minValue);
            if (value >= minValue){
                view.setTextColor(App.app.getResources().getColor(R.color.black));
            }else{
                view.setTextColor(App.app.getResources().getColor(R.color.red));
            }
        }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
            double maxValue = Double.parseDouble(this.maxValue);
            if (value <= maxValue){
                view.setTextColor(App.app.getResources().getColor(R.color.black));
            }else{
                view.setTextColor(App.app.getResources().getColor(R.color.red));
            }
        }
    }
}
